package com.springboot.controller;

import com.springboot.model.UserLoginForm;
import com.springboot.service.AssistantSendMessageService;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 本控制台用于助理提醒患者就诊
 * 给该患者发送信息
 */
@Controller
public class AssistantSendMessage {
    @Autowired
    AssistantSendMessageService assistantSendMessageService;
    /**
     * 助理提醒患者就诊
     */
    @CrossOrigin
    @RequestMapping(value = "/assistantSendMessageServlet")
    public @ResponseBody
    String assistantSendMessage(@RequestParam("doctorTel") String doctorTel,
                                @RequestParam("patientTel") String patientTel,
                                @RequestParam("bookDate") String bookDate,
                                @RequestParam("bookTime") String bookTime) {
        System.out.println("助理提醒患者就诊");
        try {
            UserLoginForm doctor=assistantSendMessageService.findUserByTel(doctorTel);
            String doctorName=doctor.getUserName();
            UserLoginForm patient=assistantSendMessageService.findUserByTel(patientTel);
            String patientName=patient.getUserName();
            ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com", "106819", "bb593efe-c877-4ece-b5f6-b30f5de7733b");
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number",patientTel);
            params.put("templateId", "2563"); //2563短信模板，用于发送就诊提醒
            String[] templateParams = new String[4];
            templateParams[0] = patientName;
            templateParams[1] = doctorName;
            templateParams[2] = bookDate;
            templateParams[3] = bookTime;
            params.put("templateParams", templateParams);
            String result = null;
            try {
                result = client.send(params);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String balance = null;
            try {
                balance = client.balance();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("result = " + result);
            System.out.println("balance = " + balance);
            return "True";
        } catch (Exception e) {
            return "False";
        }
    }
}
